var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 100,
    list_type:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 隐藏分享
    wx.hideShareMenu()
  },

  // 订单分类
  changeType: function (e) {
    var types = e.currentTarget.dataset.type;
    this.setData({
      page: 1,
      list_type: types
    })
    wx.showLoading({
      title: '加载中',
    })

    this.newpm();

    if (this.data.list_type == 1) {
      this.goodsMsgList();
    } else if (this.data.list_type == 2) {
      this.innMsgList();
    }

  },
  onShow:function(){
    this.newpm();
    if (this.data.list_type==1){
      this.goodsMsgList();
    } else if (this.data.list_type == 2){
      this.innMsgList();
    }
  },

  //是否有新消息
  newpm:function(){
    var that = this;
    api.POST({
      params: {},
      url: app.globalData.base_url + 'inn/newpm',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            newGoodsMsg: res.data.data.newGoodsMsg,
            newInnMsg: res.data.data.newInnMsg
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  //小店消息列表
  innMsgList: function () {
    var that = this;
    var page = this.data.page;
    var limit = this.data.limit;
    var params = {
      limit: that.data.limit,
      page: page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'inn/innMsgList',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var innMsgList = res.data.data.list;
          if (page == 1) {
            var itemData = innMsgList;
          } else {
            var itemData = that.data.innMsgList.concat(innMsgList)
          }

          that.setData({
            total_page: totalPage,
            innMsgList: itemData
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },


  //商品消息列表
  goodsMsgList: function () {
    var that = this;
    var page = this.data.page;
    var limit = this.data.limit;
    var params = {
      limit: that.data.limit,
      page: page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'inn/goodsMsgList',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var goodsMsgList = res.data.data.list;
          if (page == 1) {
            var itemData = goodsMsgList;
          } else {
            var itemData = that.data.goodsMsgList.concat(goodsMsgList)
          }

          that.setData({
            total_page: totalPage,
            goodsMsgList: itemData
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  shopLink:function(e){
    // console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/agent/pages/storeGoodsList/storeGoodsList?inn_id='+id
    })

  },

  goodsLink: function (e) {
    var sku_code = e.currentTarget.dataset.sku_code;
    var inn_id = e.currentTarget.dataset.inn_id;
    wx.navigateTo({
      url: '/agent/pages/storeProductDetail/storeProductDetail?sku_code=' + sku_code + '&inn_id=' + inn_id
    })

  },



  //页面下拉刷新
  onPullDownRefresh: function () {
    this.refresh();
  },

  //刷新数据
  refresh: function () {
    var that = this;
    that.setData({
      page: 1,
    });
    setTimeout(function () {
      that.getWaiterList()
    }, 1000)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      page: this.data.page + 1
    });
    setTimeout(function () {
      that.getWaiterList()
    }, 500)
  },
})